package cn.xiqi.cms.hibernate.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xiqi.cms.hibernate.DAO.LogDAO;
import cn.xiqi.cms.hibernate.beans.Loginfo;

public class LogServiceImpl implements LogService {
	private LogDAO dao;

	@Override
	public List<Loginfo> getAll(int page, int size) {
		List<Loginfo> records;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "all");
		if(page != 0){
			values.put("page", page);
			values.put("size", size);
		}
		records = dao.select(values);
		return records;
	}

	@Override
	public List<Loginfo> getByID(int id) {
		List<Loginfo> records;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "id");
		values.put("id", id);
		records = dao.select(values);
		return records;
	}

	@Override
	public int add(int type, int usrID, Date logtime, String content) {
		int id;
		Loginfo record = new Loginfo(type, usrID, logtime, content);
		
		id = dao.insert(record);
		return id;
	}

	@Override
	public List<Loginfo> getByTime(Date start, Date end, int page, int size) {
		List<Loginfo> records;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "time");
		values.put("start", start);
		values.put("end", end);
		if(page != 0){
			values.put("page", page);
			values.put("size", size);
		}
		records = dao.select(values);
		return records;
	}

	@Override
	public List<Loginfo> getByType(int type, int page, int size) {
		List<Loginfo> records;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "type");
		values.put("typeid", type);
		if(page != 0){
			values.put("page", page);
			values.put("size", size);
		}
		records = dao.select(values);
		return records;
	}

	@Override
	public List<Loginfo> getByUsr(int userid, int page, int size) {
		List<Loginfo> records;
		Map<String, Object> values = new HashMap<>();
		
		values.put("type", "user");
		values.put("user", userid);
		if(page != 0){
			values.put("page", page);
			values.put("size", size);
		}
		records = dao.select(values);
		return records;
	}
	
	@Override
	public boolean delete(Loginfo record) {
		boolean b;
		
		b = dao.delete(record);
		return b;
	}

	@Override
	public void setDao(LogDAO dao) {
		this.dao = dao;
	}

}
